from exts import db
from datetime import datetime

# 栏目
class Column(db.Model):
    __tablename__ = 'column'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    fid = db.Column(db.Integer, default=0)
    title = db.Column(db.String(20), unique=True)

    def to_json(self):
        json_post = {
            'id': self.id,
            'fid': self.fid,
            'title': self.title
        }
        return json_post

# 管理员
class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 默认管理员账号为admin
    name = db.Column(db.String(11), default='admin')
    # 默认管理员密码为admin(md5加密）
    password = db.Column(db.String(32), default='21232f297a57a5a743894a0e4a801fc3')
    icon = db.Column(db.String(150))
    time = db.Column(db.DateTime)

# 文章
class Blog(db.Model):
    __tablename__ = 'blog'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(150), nullable=False, unique=True)
    content = db.Column(db.Text)
    article_intro = db.Column(db.String(200))
    image = db.Column(db.String(150))
    uid = db.Column(db.Integer)
    fid = db.Column(db.Integer)
    read = db.Column(db.Integer, default=0)
    time = db.Column(db.DateTime, default=datetime.now())

    def to_json(self):
        json_post = {
            'id': self.id,
            'fid': self.fid,
            'uid': self.uid,
            'title': self.title,
            'content': self.content,
            'image': self.image,
            'read': self.read,
            'time': self.time
        }
        return json_post

# 友情链接
class Youlian(db.Model):
    __tablename__ = 'youlian'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(10), nullable=False)
    url = db.Column(db.String(70))
